// @ts-ignore
import React, { useState, useEffect } from 'react';
import { observer } from "mobx-react";

import ModelStore from "../index/model";
import FingerDevice from "../finger_device/model";

import { Button } from "antd"
import '../app.less';
import {Link} from "umi";
import {sys} from "@/utils/sys";
const clients = [];
let serverStatus;
let timer;
const nw = require('nw.gui');

export default observer(() => {
    let [a, setA] = useState("开始录入");


    useEffect(() => {
        FingerDevice.addCloseConfirm()
        FingerDevice.createServer()
    }, [])

    return (
        <div>
            <h1>Hello, World!{a}</h1>
            <button onClick={() => {
                FingerDevice.send('你好')
            }}>发送</button>

            <button onClick={() => {
                FingerDevice.test()
            }}>{FingerDevice.testText}</button>

            <button onClick={() => {

                ModelStore.show()

            }}>测试mbox</button>
            <Button onClick={() => {
                FingerDevice.WEB_LIGHT_RED()
            }}>红色灯</Button>
            <Button onClick={() => {
                FingerDevice.WEB_FINGER_SCAN()
            }}>扫描手指</Button>
            <Button onClick={() => {
                FingerDevice.WEB_FINGER_CMP()
            }}>对比指纹</Button>
            <Button onClick={() => {
                FingerDevice.WEB_DEVICE_STATUS()
                FingerDevice.testText="123123"
                sys.setItem("aaa","asdfasdf")
                // 创建一个新的窗口
                const win = nw.Window.open('index.html#test2'+sys.jsonToUrlData({
                    aa:123123,
                    bb:334534,
                    dd:"232344232344232344232344232344232344232344232344232344232344232344asdfasdfasdfasdffsrtylrktyjertylertyerkltyrretyrty.wertwertwertert#$$asdfljkajewlkgewklljkejkrgklegjklwejklrglkjegrkgkjwerjklgjl;ekrjgl;ekjrgl;kejrgl;kwejrglkjwergl;kjwerlgkjwerlkjgwelkrgjlwekrjgoneinewrfinweriufniewfnweirfnwfkqwelrjqwelkr"
                }), {
                    frame: false, // 设置为无边框
                    width: 800,
                    height: 600,
                    position:"center",
                    new_instance: true,
                    resizable: false,
                    show_in_taskbar:false,
                    always_on_top:true,
                });
                // 监听窗口加载事件
                // win.on('loaded', function() {
                //
                //     console.log('窗口已加载');
                // });
                win.on('close',()=>{

                    console.log('等待数据保存后，再关闭--');
                })
            }}>打开新窗口</Button>
            <Button onClick={()=>{

                const win = nw.Window.get();
                win.close();
            }}>关闭当期窗口</Button>
            <Button onClick={()=>{
                const win = nw.Window.get();
                nw.Window.getAll((windows)=>{
                    for (let i = 0; i < windows.length; i++) {
                        if (windows[i] !== win) {
                            console.log(windows[i])
                            windows[i].close(true);
                        }
                    }
                });
                win.close(true);
            }}>关闭所有窗口</Button>
            <Button onClick={()=>{
                FingerDevice.writeFile("config.txt","1|12323313232")
            }}>创建文件</Button>
            <Button onClick={ async ()=>{
                let blob = new Blob(['Hello, world!'], {type: 'text/plain'});
                FingerDevice.writeBlobFile("config.txt",blob)
            }}>blob创建文件</Button>
        </div>
    );
});
